# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load("//pw_build:compatibility.bzl", "incompatible_with_mcu")
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
    features = [
        "-layering_check",
        "-ctad_warnings",
    ],
)

licenses(["notice"])

cc_library(
    name = "server_api",
    srcs = [
        "server_reader_writer.cc",
    ],
    hdrs = [
        "public/pw_rpc/pwpb/internal/method.h",
        "public/pw_rpc/pwpb/internal/method_union.h",
        "public/pw_rpc/pwpb/server_reader_writer.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":common",
        "//pw_bytes",
        "//pw_function",
        "//pw_rpc",
        "//pw_rpc/raw:server_api",
        "//pw_span",
        "//pw_status",
    ],
)

cc_library(
    name = "client_api",
    hdrs = [
        "public/pw_rpc/pwpb/client_reader_writer.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":common",
        "//pw_bytes",
        "//pw_function",
        "//pw_rpc",
    ],
)

cc_library(
    name = "common",
    hdrs = [
        "public/pw_rpc/pwpb/internal/common.h",
        "public/pw_rpc/pwpb/serde.h",
    ],
    strip_include_prefix = "public",
    deps = [
        "//pw_assert:assert",
        "//pw_bytes",
        "//pw_protobuf",
        "//pw_rpc",
        "//pw_span",
        "//pw_status",
        "//pw_stream",
    ],
)

cc_library(
    name = "test_method_context",
    hdrs = [
        "public/pw_rpc/pwpb/fake_channel_output.h",
        "public/pw_rpc/pwpb/test_method_context.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":common",
        ":server_api",
        "//pw_containers:wrapped_iterator",
        "//pw_preprocessor",
        "//pw_rpc",
        "//pw_rpc:fake_channel_output",
        "//pw_rpc:internal_test_utils",
        "//pw_span",
    ],
)

cc_library(
    name = "client_testing",
    hdrs = [
        "public/pw_rpc/pwpb/client_testing.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":test_method_context",
        "//pw_bytes",
        "//pw_rpc",
        "//pw_rpc/raw:client_testing",
    ],
)

cc_library(
    name = "client_server_testing",
    hdrs = [
        "public/pw_rpc/pwpb/client_server_testing.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":test_method_context",
        "//pw_assert:assert",
        "//pw_rpc:client_server_testing",
    ],
)

cc_library(
    name = "client_server_testing_threaded",
    hdrs = [
        "public/pw_rpc/pwpb/client_server_testing_threaded.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":test_method_context",
        "//pw_rpc:client_server_testing",
        "//pw_rpc:client_server_testing_threaded",
    ],
)

cc_library(
    name = "internal_test_utils",
    hdrs = ["pw_rpc_pwpb_private/internal_test_utils.h"],
    deps = [
        "//pw_span",
        "//pw_status",
        "//pw_stream",
    ],
)

cc_library(
    name = "echo_service",
    hdrs = ["public/pw_rpc/echo_service_pwpb.h"],
    strip_include_prefix = "public",
    deps = [
        "//pw_rpc:echo_pwpb_rpc",
    ],
)

# TODO: b/242059613 - Enable this library when logging_event_handler can be used.
filegroup(
    name = "client_integration_test",
    srcs = [
        "client_integration_test.cc",
    ],
    #deps = [
    #    "//pw_rpc:integration_testing",
    #    "//pw_sync:binary_semaphore",
    #    "//pw_rpc:benchmark_pwpb_rpc",
    #]
)

pw_cc_test(
    name = "client_call_test",
    srcs = [
        "client_call_test.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":client_api",
        ":internal_test_utils",
        "//pw_rpc:internal_test_utils",
        "//pw_rpc:pw_rpc_test_pwpb",
    ],
)

pw_cc_test(
    name = "client_reader_writer_test",
    srcs = [
        "client_reader_writer_test.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":client_api",
        ":client_testing",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
    ],
)

pw_cc_test(
    name = "client_server_context_test",
    srcs = [
        "client_server_context_test.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":client_server_testing",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
        "//pw_sync:mutex",
    ],
)

pw_cc_test(
    name = "client_server_context_threaded_test",
    srcs = [
        "client_server_context_threaded_test.cc",
    ],
    features = ["-conversion_warnings"],
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        ":client_server_testing_threaded",
        "//pw_function",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
        "//pw_status",
        "//pw_sync:binary_semaphore",
        "//pw_thread:non_portable_test_thread_options",
        "//pw_thread_stl:non_portable_test_thread_options",
    ],
)

pw_cc_test(
    name = "codegen_test",
    srcs = [
        "codegen_test.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":internal_test_utils",
        ":test_method_context",
        "//pw_preprocessor",
        "//pw_rpc",
        "//pw_rpc:internal_test_utils",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
    ],
)

pw_cc_test(
    name = "fake_channel_output_test",
    srcs = ["fake_channel_output_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":test_method_context",
        "//pw_rpc",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
    ],
)

pw_cc_test(
    name = "method_test",
    srcs = ["method_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":internal_test_utils",
        ":server_api",
        "//pw_containers:algorithm",
        "//pw_rpc",
        "//pw_rpc:internal_test_utils",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
    ],
)

pw_cc_test(
    name = "method_info_test",
    srcs = ["method_info_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        "//pw_rpc",
        "//pw_rpc:internal_test_utils",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
        "//pw_status",
    ],
)

pw_cc_test(
    name = "method_lookup_test",
    srcs = ["method_lookup_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":test_method_context",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
        "//pw_rpc/raw:test_method_context",
    ],
)

pw_cc_test(
    name = "method_union_test",
    srcs = ["method_union_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":internal_test_utils",
        ":server_api",
        "//pw_rpc",
        "//pw_rpc:internal_test_utils",
        "//pw_rpc:pw_rpc_test_pwpb",
    ],
)

pw_cc_test(
    name = "echo_service_test",
    srcs = ["echo_service_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":echo_service",
        ":test_method_context",
    ],
)

pw_cc_test(
    name = "server_reader_writer_test",
    srcs = ["server_reader_writer_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":test_method_context",
        "//pw_rpc",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
    ],
)

pw_cc_test(
    name = "serde_test",
    srcs = ["serde_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":common",
        "//pw_rpc:pw_rpc_test_pwpb",
        "//pw_span",
        "//pw_status",
    ],
)

pw_cc_test(
    name = "server_callback_test",
    srcs = ["server_callback_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":test_method_context",
        "//pw_containers:vector",
        "//pw_rpc",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
    ],
)

pw_cc_test(
    name = "stub_generation_test",
    srcs = ["stub_generation_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
    ],
)

pw_cc_test(
    name = "synchronous_call_test",
    srcs = ["synchronous_call_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":test_method_context",
        "//pw_chrono:system_clock",
        "//pw_rpc",
        "//pw_rpc:pw_rpc_test_pwpb_rpc",
        "//pw_rpc:synchronous_client_api",
        "//pw_rpc_transport:test_loopback_service_registry",
        "//pw_status",
        "//pw_thread:thread",
        "//pw_work_queue",
        "//pw_work_queue:stl_test_thread",
        "//pw_work_queue:test_thread_header",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "docs.rst",
    ],
    target_compatible_with = incompatible_with_mcu(),
)
